/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controllers;

import db.OrderDb;
import db.OrderInterface;
import db.ProductDb;
import db.ProductInterface;
import java.util.ArrayList;
import models.Customer;
import models.Employee;
import models.Order;
import models.OrderItem;
import models.Product;

/**
 *
 * @author pelle
 */
public class OrderController {

    private Order order = null;
    private OrderInterface orderDb = new OrderDb();
    private ProductInterface productDb = new ProductDb();

    public OrderController() {
        this.order = new Order();
    }

    public OrderController(int orderId) {
        Order thisOrder = orderDb.getOrder(orderId);
        if(thisOrder != null) {
            this.order = thisOrder;
        }
    }

    public Order getOrder() {
        return this.order;
    }

    public ArrayList<OrderItem> getOrderItems() {
        return order.getOrderItems();
    }

    public boolean addProduct(int productId) {
        Product product = productDb.getProduct(productId);
        boolean added = false;
        
        if(product != null) {
            added = this.order.addProduct(product, 1);
        }
        return added;
    }

    public boolean addProduct(Product product) {
        boolean added = false;

        if(product != null) {
            added = this.order.addProduct(product, 1);
        }

        return added;
    }

    public void minusQuantity(Product product) {
        if(product != null) {
            this.order.removeOneQuantity(product);
        }
    }

    public boolean removeProduct(Product product) {
        return this.order.removeProduct(product);
    }

    public ArrayList<Product> searchProducts(String search) {
        return productDb.searchProducts(search);
    }

    public void setCustomer(Customer customer) {
        order.setCustomer(customer);
    }

    public void setEmployee(Employee employee) {
        order.setEmployee(employee);
    }

    public Customer getCustomer() {
        return order.getCustomer();
    }

    public Employee getEmployee() {
        return order.getEmployee();
    }

    public int createOrder() {
        int orderId = -1;
        orderId = this.orderDb.saveOrder(order);
        return orderId;
    }

    public boolean updateOrder() {
        boolean success = false;
        success = this.orderDb.updateOrder(order);
        return success;
    }

}
